﻿using System;
using NUnit.Framework;

namespace PropertyExpression.TypedSqlBuilder.Test
{
    [TestFixture]
    public partial class SqlTest
    {
        [Test]
        public void Test0()
        {
            var query = 
                From(SalesOrderHeaders, salesOrderHeader => 
                SelectSalesOrderHeaderProjection(
                    salesOrderHeader.OrderDate()
                ));

            Console.WriteLine(query);
        }

        [Test]
        public void Test1()
        {
            var query =
                From(SalesOrderHeaders, salesOrderHeader => To(
                    Where(salesOrderHeader.SalesOrderID().Equal(salesOrderHeader.SalesOrderID())),
                SelectSalesOrderHeaderProjection(
                    salesOrderHeader.OrderDate()
                )));

            Console.WriteLine(query);
        }

        [Test]
        public void Test2()
        {
            var query = 
                From(SalesOrderHeaders, salesOrderHeader => 
                    SalesOrderDetails.InnerJoin(salesOrderDetail => To(
                        salesOrderHeader.SalesOrderID().Equal(salesOrderDetail.SalesOrderID()),
                SelectSalesOrderHeaderProjection(
                    salesOrderHeader.OrderDate()
                ))));

            Console.WriteLine(query);
        }

        [Test]
        public void Test3()
        {
            var query =
                From(SalesOrderHeaders, salesOrderHeader => 
                    InnerJoin(SalesOrderDetails, salesOrderDetail => To(
                        salesOrderHeader.SalesOrderID().Equal(salesOrderDetail.SalesOrderID()),
                Where(salesOrderHeader.SalesOrderID().Equal(salesOrderHeader.SalesOrderID())),
                SelectSalesOrderHeaderProjection(
                    salesOrderHeader.OrderDate()
                ))));

            Console.WriteLine(query);
        }

        [Test]
        public void Test4()
        {
            var query = NewMethod1("", "", DateTime.Now, DateTime.Now);
            Console.WriteLine(query);
        }

        private static IFrom<SalesOrderHeaderProjection> NewMethod1(
            string productName, string contactFirstName, 
            DateTime startOrderDate, DateTime endOrderDate)
        {
            return
                From(SalesOrderHeaders, salesOrderHeader =>
                    InnerJoin(SalesOrderDetails, salesOrderDetail => To(
                        salesOrderHeader.SalesOrderID().Equal(salesOrderDetail.SalesOrderID()),
                    InnerJoin(SpecialOfferProducts, specialOfferProduct => To(
                        salesOrderDetail.SpecialOfferID().Equal(specialOfferProduct.SpecialOfferID())
                        .And(salesOrderDetail.ProductID().Equal(specialOfferProduct.ProductID())),
                    InnerJoin(SpecialOffers, specialOffer => To(
                        specialOfferProduct.SpecialOfferID().Equal(specialOffer.SpecialOfferID()),
                    InnerJoin(Products, product => To(
                        specialOfferProduct.ProductID().Equal(product.ProductID()),
                    LeftOuterJoin(Individuals, individual => To(
                        salesOrderHeader.CustomerID().Equal(individual.CustomerID()),
                    LeftOuterJoin(Contacts, contact => To(
                        individual.ContactID().Equal(contact.ContactID()),
                Where(
                    product.Name().Like(productName.Const())
                    .And(
                    contact.FirstName().Like(contactFirstName.Const()))
                    .And(
                    salesOrderHeader.OrderDate().Between(startOrderDate.Const(), endOrderDate.Const()))
                    ),
                Select<SalesOrderHeaderProjection>()
                    .OrderDate(salesOrderHeader.OrderDate())
                    .ProductName(product.Name())
                    .ContactFirstName(contact.FirstName())
                )))))))))))));
        }

        //[Test]
        //public void Test5()
        //{
        //    var query =
        //        From(SalesOrderHeaders, salesOrderHeader =>
        //            InnerJoin(SalesOrderDetails, salesOrderDetail => To(
        //                salesOrderHeader.SalesOrderID().Equal(salesOrderDetail.SalesOrderID()),
        //            InnerJoin(SpecialOfferProducts, specialOfferProduct => To(
        //                salesOrderDetail.SpecialOfferID().Equal(specialOfferProduct.SpecialOfferID())
        //                .And(salesOrderDetail.ProductID().Equal(specialOfferProduct.ProductID())),
        //            RightOuterJoin(SpecialOffers, specialOffer => To(
        //                specialOfferProduct.SpecialOfferID().Equal(specialOffer.SpecialOfferID()),
        //            InnerJoin(Products, product => To(
        //                specialOfferProduct.ProductID().Equal(product.ProductID()),
        //            InnerJoin(Individuals, individual => To(
        //                salesOrderHeader.CustomerID().Equal(individual.CustomerID()),
        //            InnerJoin(Contacts, contact => To(
        //                individual.ContactID().Equal(contact.ContactID()),
        //        Where(
        //            salesOrderHeader.SalesOrderID().Equal(salesOrderHeader.SalesOrderID())
        //            ),
        //        SelectSalesOrderHeaderOuterProjection(
        //            salesOrderHeader.OrderDate()
        //        ))))))))))))));

        //    Console.WriteLine(query);
        //}

        //[Test]
        //public void Test6()
        //{
        //    var query =
        //        From(SalesOrderHeaders, salesOrderHeader =>
        //            InnerJoin(SalesOrderDetails, salesOrderDetail => To(
        //                salesOrderHeader.SalesOrderID().Equal(salesOrderDetail.SalesOrderID()),
        //            InnerJoin(SpecialOfferProducts, specialOfferProduct => To(
        //                salesOrderDetail.SpecialOfferID().Equal(specialOfferProduct.SpecialOfferID())
        //                .And(salesOrderDetail.ProductID().Equal(specialOfferProduct.ProductID())),
        //            RightOuterJoin(SpecialOffers, specialOffer => To(
        //                specialOfferProduct.SpecialOfferID().Equal(specialOffer.SpecialOfferID()),
        //            InnerJoin(Products, product => To(
        //                specialOfferProduct.ProductID().Equal(product.ProductID()),
        //            RightOuterJoin(Individuals, individual => To(
        //                salesOrderHeader.CustomerID().Equal(individual.CustomerID()),
        //            RightOuterJoin(Contacts, contact => To(
        //                individual.ContactID().Equal(contact.ContactID()),
        //        Where(
        //            salesOrderHeader.SalesOrderID().Equal(salesOrderHeader.SalesOrderID())
        //            ),
        //        SelectSalesOrderHeaderOuterProjection(
        //            salesOrderHeader.OrderDate()
        //        ))))))))))))));

        //    Console.WriteLine(query);
        //}
    }
}
